package day04;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MyClass {
    public static void main(String[] args) throws NoSuchFieldException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {

        //Student类的class对象
        Class<Student> studentClass = Student.class;
        System.out.println(studentClass);

        //获取成员变量
        Field dname = studentClass.getDeclaredField("name");
        Field did = studentClass.getDeclaredField("id");
        //取消访问检查
        dname.setAccessible(true);
        did.setAccessible(true);
       //建立学生对象
        Student student = studentClass.newInstance();
        //给变量赋值
        dname.set(student,"张三");
        did.set(student,"18");
        //打印学生对象
        System.out.println(student);

       //打印所有的方法
        Method[] methods = studentClass.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        //获取hashCode方法
        Method hashCode = studentClass.getMethod("hashCode");
        //使用student对象运行hashCode方法
        Object invoke = hashCode.invoke(student);
        //输入hashCode的返回值
        System.out.println(invoke);

    }
}
